package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.QuanZiCommentApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.CommentVo;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QuanZiCommentService {
    @Reference(version = "1.0.0")
    private QuanZiCommentApi quanZiCommentApi;
    @Autowired
    private UserInfoMapper userInfoMapper;

    public PageResult queryCommentsList(String publishId, Integer page, Integer pageSize) {
        User user = UserThreadLocal.get();
        Long userId = user.getId();
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        List<CommentVo> commentVos = new ArrayList<>();
        //查询评论表
        List<Comment> comments = quanZiCommentApi.queryCommets(publishId);
        List<Long> userInfoIds = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVo commentVo = new CommentVo();
            commentVo.setId(comment.getId().toHexString());
            commentVo.setContent(comment.getContent());
            commentVos.add(commentVo);
            userInfoIds.add(comment.getUserId());
        }
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", userInfoIds);
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);
        for (UserInfo userInfo : userInfos) {
            for (int i = 0; i < comments.size(); i++) {
                if (userInfo.getUserId().equals(comments.get(i).getUserId())) {
                    CommentVo commentVo = commentVos.get(i);
                    commentVo.setAvatar(userInfo.getLogo());
                    commentVo.setNickname(userInfo.getNickName());
                    commentVo.setCreateDate(DateUtil.format(new Date(comments.get(i).getCreated()), "HH:mm"));
                    //查询本条评论有多少人点赞
                    Integer likeCount = quanZiCommentApi.queryLikeCount(commentVo.getId());
                    commentVo.setLikeCount(likeCount);
                    boolean isLiked = quanZiCommentApi.queryUserIsLiked(userId, commentVo.getId());
                    commentVo.setHasLiked(isLiked ? 1 : 0);
                    break;
                }
            }
        }
        pageResult.setItems(commentVos);
        return pageResult;
    }
}
